﻿package com.shuqi.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 登录信息保存类
 * 
 * @author Administrator
 * 
 */
public class LoginHelper {

    public static String decryptWithFlag(String sSrc, String sKey, boolean isRealdecry) {

        if (isRealdecry) {
            try {
                return decrypt(sSrc, sKey);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        } else
            return sSrc;
    }

    public static String encryptWithFlag(String sSrc, String sKey, boolean isRealencry) {
        if (isRealencry) {
            try {
                return encrypt(sSrc, sKey);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        } else {
            return sSrc;
        }
    }

    /**
     * 解密
     * 
     * @param sSrc 要解密的源
     * @param sKey 密钥
     * @return
     * @throws Exception
     */
    public static String decrypt(String sSrc, String sKey) throws Exception {
        try {
            if (sKey == null) {
                return null;
            }
            if (sKey.length() != 16) {
                return null;
            }
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            if (sSrc == null) {
                return null;
            }
            int l = sSrc.length();
            if (l % 2 != 0) {
                return null;
            }
            byte[] b = new byte[l / 2];
            for (int i = 0; i != l / 2; i++) {
                b[i] = (byte) Integer.parseInt(sSrc.substring(i * 2, i * 2 + 2), 16);
            }
            byte[] encrypted1 = b;
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                return originalString;
            } catch (Exception e) {
                return null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 加密
     * 
     * @param sSrc 要加密的源
     * @param sKey 密钥
     * @return
     * @throws Exception
     */
    public static String encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            return null;
        }
        if (sKey.length() != 16) {
            return null;
        }
        byte[] raw = sKey.getBytes("ASCII");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes());
        StringBuilder hs = new StringBuilder();
        String stmp = "";
        for (int n = 0; n < encrypted.length; n++) {
            stmp = (java.lang.Integer.toHexString(encrypted[n] & 0XFF));
            if (stmp.length() == 1) {
                hs.append("0" + stmp);
            } else {
                hs.append(stmp);
            }
        }
        return hs.toString().toUpperCase();
    }

    /**
     * SHA-1加密
     * 
     * @param strSrc
     * @param encName
     * @return
     */
    public static String Encrypt(String strSrc) {
        // parameter strSrc is a string will be encrypted,
        // parameter encName is the algorithm name will be used.
        // encName dafault to "MD5"
        MessageDigest md = null;
        String strDes = "";

        byte[] bt = strSrc.getBytes();
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(bt);
            // strDes = bytes2Hex(md.digest());
            // to HexString
            byte[] bts = md.digest();
            String tmp = null;
            for (int i = 0; i < bts.length; i++) {
                tmp = (Integer.toHexString(bts[i] & 0xFF));
                if (tmp.length() == 1) {
                    strDes += "0";
                }
                strDes += tmp;
            }
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Invalid algorithm.");
            return null;
        }
        return strDes;
    }
}
